package main;

import java.util.*;

import base.Room;
import base.Window;

public class RoomLibrary {
	private static Vector<Vector<Room>> roomNames = new Vector<Vector<Room>>( );
	private static Vector<String> roomNamesNames = new Vector<String>( );
	private static Hashtable<String,Integer> rooms = new Hashtable<String,Integer>( );
	private static Hashtable<String,String> roomImages = new Hashtable<String,String>( );
	private static Hashtable<String,Window> windows = new Hashtable<String,Window>( );

	public static void add( String name , String image )
	{
		if( !rooms.containsKey( name ) )
		{
			Vector<Room> temp = new Vector<Room>( );
			temp.add( new Room( ) );
			roomImages.put( name , image );
			roomNamesNames.add( name );
			rooms.put( name , roomNames.size( ) );
			roomNames.add( new Vector<Room>( ) );
			//return;
		}
		//int index = rooms.get( name );
		//System.out.println( "ADDING ROOM: " + name + " at: " + index );
		//int index = rooms.get( name );
		roomNames.get( rooms.get( name ) ).add( new Room( ) );
		//rooms.get( name ).add( new Room( ) );
	}

	public static String getImage( String name )
	{
		return roomImages.get( name );
	}

	public static Room getRoom( String name , int index )
	{
		return roomNames.get( rooms.get( name ) ).get( index );
		///return rooms.get( roomNames.get( name ) ).get( index );
	}

	public static void addDevice( String room , int index , Window device )
	{
		windows.put( device.getName( ) , device );//TODO COPY??
		roomNames.get( rooms.get( room ) ).get( index ).addDevice( device );
		//rooms.get( room ).get( index ).addDevice( device );
	}

	public static int getNumberOfRooms( )
	{
		return roomNames.size( );
	}

	public static int getNumberOfRooms( String room )
	{
		return roomNames.get( rooms.get( room ) ).size( );
	}

	public static int getNumberOfRooms( int index )
	{
		return roomNames.get( index ).size( );
	}

	public static String getName( int index )
	{
		return roomNamesNames.get( index );
		/*Set<String> keys = rooms.keySet( );
		int id = 0;
		for( String key : keys )
		{
			if( id == index )
				return key;
			++id;
		}
		return null;*/
	}

	public static boolean isFavorite( String name )
	{
		if( roomNames.get( 0 ).get( 0 ).devices.containsKey( name ) )
			return true;
		return false;
	}

	public static void removeFavorite( String name )
	{
		roomNames.get( 0 ).get( 0 ).devices.remove( name );
	}

	public static void addFavorite( String name )
	{
		Room fav = roomNames.get( 0 ).get( 0 );
		if( fav.devices.containsKey( name ) )
			return;

		fav.addDevice( windows.get( name ) );
	}

	public static String getImage( int index )
	{
		//System.out.println( "name = " + getName( index ) );
		return roomImages.get( getName( index ) );
	}

	public static Collection<Window> getDevices( String name , int index )
	{
		//System.out.println( "Trying to get devices from " + name + " at index " + index );
		if( rooms.get( name ) == null )
			return null;
		if( roomNames.get( rooms.get( name ) ) == null )
			return null;
		if( roomNames.get( rooms.get( name ) ).get( index ) == null )
			return null;
		if( roomNames.get( rooms.get( name ) ).get( index ).devices == null )
			return null;
		if( roomNames.get( rooms.get( name ) ).get( index ).devices.size( ) == 0 )
			return null;
		return roomNames.get( rooms.get( name ) ).get( index ).devices.values( );
	}
}
